package ui.commodity;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import tool.JTableExportListener;
import tool.LongStringTransformer;
import tool.MyButton;
import vo.CommodityVo;
import vo.UserVo;

import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;






import client.boot.BLServiceManager;
import blservice.commodity.CommodityBLService;

import java.awt.Font;
import java.io.File;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import java.awt.Color;

public class StockCheckPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	File file=new File("库存盘点.txt");
	CommodityBLService commodityBL;
    UserVo user;
    private JTable table;
    private JScrollPane jsp;
    Vector<String> rowData;
	Vector<String> stockInfo;
	DefaultTableModel dtm;
    
    
	public StockCheckPanel(UserVo user) {
		this.user=user;
		this.setLayout(null);
		this.setOpaque(false);
		
		try {
			commodityBL=BLServiceManager.getCommodityBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e) {
			JOptionPane.showMessageDialog(this,  e.getMessage());
			e.printStackTrace();
		}
		
        table = new JTable(){

			private static final long serialVersionUID = 6520960960659987619L;

			@Override
        	public boolean isCellEditable(int row, int column) {
        		return false;
        	}
        };
		
		showQuickshot();

		table.setBounds(47, 128, 535, 238);
		jsp = new JScrollPane(table);
		jsp.setBounds(49, 49, 548, 334);
		jsp.setVisible(true);
		this.add(jsp);
		
		JLabel label = new JLabel("库存快照");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(295, 10, 80, 21);
		add(label);	
		
		JButton btnExport = new MyButton("Graphics/Button93-23.png");
		btnExport.setText("导出");
		btnExport.addActionListener(new JTableExportListener(table,"库存快照",""));
		btnExport.setBounds(504, 393, 93, 23);
		add(btnExport);
			
	}
	
	private void showQuickshot() {
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("行号");
		columnNames.add("商品名称");
		columnNames.add("商品型号");
		columnNames.add("库存数量");
		columnNames.add("库存均价");
//		columnNames.add("批次");
//		columnNames.add("批号");
//		columnNames.add("出厂日期");		
		dtm=new DefaultTableModel();
		dtm.setColumnIdentifiers(columnNames);
		try {
			List<CommodityVo> voList=commodityBL.getAll();
			if(voList==null){
				JOptionPane.showMessageDialog(this, "你输入的时间段内没有任何出入库记录\n");
			} else {
				for (int i = 0; i < voList.size(); i++) {
					stockInfo = new Vector<String>();
					stockInfo.add(Integer.toString(i+1));
					stockInfo.add(voList.get(i).getName());
					stockInfo.add(voList.get(i).get型号());				
					stockInfo.add(Integer.toString(voList.get(i).getstocknum()));					
					stockInfo.add(LongStringTransformer.LongToString(voList.get(i).get库存均价()));
				
					dtm.addRow(stockInfo);
				}
			}
			table.setModel(dtm);
		} catch (RemoteException e1) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器，显示库存盘点结果失败\n" + e1.getMessage());
			e1.printStackTrace();
		}			
	}
}
